package com.ucw.ucwclient.biz.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ucw.ucwclient.dal.dataobject.Account;
import com.ucw.ucwclient.dal.dataobject.UActivity;
import com.ucw.ucwclient.dal.dataobject.UActivityCollect;
import com.ucw.ucwclient.dal.dataobject.UActivityEnroll;
import com.ucw.ucwclient.dal.mapper.AccountMapper;
import com.ucw.ucwclient.dal.mapper.UActivityCollectMapper;
import com.ucw.ucwclient.dal.mapper.UActivityEnrollMapper;
import com.ucw.ucwclient.dal.mapper.UActivityMapper;
import com.ucw.ucwclient.dal.search.NormalSearch;

@Service
public class UActivityService {

	@Resource
	private UActivityCollectMapper uActivityCollectMapper;

	@Resource
	private UActivityEnrollMapper uActivityEnrollMapper;

	@Resource
	private UActivityMapper uActivityMapper;

	@Resource
	private AccountMapper accountMapper;

	public List<UActivity> getList(NormalSearch search) {
		List<UActivity> list = uActivityMapper.selectByPageSearch(search);
		return list;
	}

	public UActivity getOne(int id) {
		return uActivityMapper.selectByPrimaryKey(id);
	}

	public Map<String, String> reportOne(Integer id) {
		UActivity us = new UActivity();
		us.setId(id);
		us.setReport(true);
		int result = uActivityMapper.updateByPrimaryKey(us);
		Map<String, String> resp = new HashMap<String, String>();
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");

		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> collectOne(Integer id, Long userId) {
		UActivityCollect uac = new UActivityCollect();
		uac.setActivityId(id);
		uac.setCreateTime(new Date());
		uac.setUserId(userId);
		int result = uActivityCollectMapper.collectOne(uac);
		Map<String, String> resp = new HashMap<String, String>();

		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");

		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> cancelCollectOne(Integer id, Long userId) {
		int result = uActivityCollectMapper.deleteByPrimaryKey(id, userId);
		Map<String, String> resp = new HashMap<String, String>();

		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");

		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> enrollOne(Integer id, Long userId) {
		UActivityEnroll enroll = new UActivityEnroll();
		enroll.setActivityId(id);
		enroll.setUserId(userId);
		enroll.setCreateTime(new Date());
		int result = uActivityEnrollMapper.enrollOne(enroll);
		Map<String, String> resp = new HashMap<String, String>();

		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");

		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> cancelEnrollOne(Integer id, Long userId) {
		int result = uActivityCollectMapper.deleteByPrimaryKey(id, userId);
		Map<String, String> resp = new HashMap<String, String>();
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");

		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public List<Account> checkOneEnroll(NormalSearch search, Long userId) {
		search.setUserId(userId);
		List<Account> list = accountMapper.selectACTEnrollByPageSearch(search);
		return list;
	}

	public Map<String, String> addOne(UActivity ua, Long userId) {
		ua.setCreateTime(new Date());
		ua.setUserId(userId);
		int result = uActivityMapper.insertSelective(ua);
		Map<String, String> resp = new HashMap<String, String>();
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");

		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	@Transactional
	public Map<String, String> approve(List<Long> userIds, Integer enrollId, Long userId) {
		int result = 0;
		Map<String, String> resp = new HashMap<String, String>();
		if (userIds != null && userIds.size() > 0) {
			for (Long long1 : userIds) {
				UActivityEnroll ua = new UActivityEnroll();
				ua.setActivityId(enrollId);
				ua.setUserId(long1);
				ua.setEnrollAccept(true);
				result = result + uActivityEnrollMapper.updateByPrimaryKeySelective(ua);
			}
		}
		if (result > 0) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");

		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

}
